<?php

namespace App\Models\User;

use App\Helpers\SelfEncrypt;
use App\Models\BaseModel;
use DC\UserCenter\Models\UserWalletModel;

class AccountFlowModel extends BaseModel
{
    protected $table = 'account_flow';

    protected $primaryKey = 'id';

    public $dateFormat = 'U';

    public function target()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel','target_uid','member_id');
    }

    public function relate()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel','relate_uid','member_id');
    }

    /**
     * @param $target_uid
     * @param $money
     * @param $inout
     * @param array $params
     * @return mixed
     */
    public function createFlow($target_uid,$money,$inout,array $params){
        $data['flow_sn'] = SelfEncrypt::createCode('FL');
        $data['relate_uid'] = $params['relate_uid'] ?? 0;
        $data['target_uid']= $target_uid;
        $data['money_type']=$params['money_type'];
        $data['inout']=$inout;
        $data['change_type']=$params['change_type'];
        $data['before_money']= $params['before_money'];
        $data['money']=$money;
        $data['after_money']=UserWalletModel::where('member_id',$target_uid)->value('money'.$params['money_type']);
        $data['comment'] = $params['comment']??get_change($params['change_type']);
        $data['status']=1;
        return self::create($data);
    }

}
